/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.yzf.wisecoder.wisecodercommon.response;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author jianghuiyong
 * @email
 * @date 2018-11-16 16:34:25
 */
public class Query<T> extends LinkedHashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    /**
     * mybatis-plus分页参数
     */
    private Page<T> page;
    /**
     * 当前页码
     */
    private int current = 1;
    /**
     * 每页条数
     */
    private int size = 10;

    private String[] ascs;

    private String[] descs;

    private static final String SIZE = "pageSize";

    public Query(Map<String, Object> params) {
        putAll(params);

        //分页参数
        if (params.get("current") != null) {
            current = Integer.parseInt((String) params.get("current"));
        }
        if (params.get(SIZE) != null) {
            size = Integer.parseInt((String) params.get(SIZE));
        }

        put("offset", (current - 1) * size);
        put("current", current);
        put(SIZE, size);

        //mybatis-plus分页
        page = new Page<>(current, size);

        if (params.get("ascs") != null) {
            ascs = (String[]) params.get("ascs");
            for (String asc : ascs) {
                page.addOrder(OrderItem.asc(asc));
            }
        }
        if (params.get("descs") != null) {
            descs = (String[]) params.get("descs");
            for (String desc : descs) {
                page.addOrder(OrderItem.desc(desc));
            }
        }

    }

    public Page<T> getPage() {
        return page;
    }

    public int getcurrent() {
        return current;
    }

    public int getSize() {
        return size;
    }
}
